package com.controller;

import com.bean.Recoding;
import com.bean.User;
import com.service.AccenderService;
import com.service.RecodingService;
import com.service.UserService;
import com.utils.SendMail;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
public class RecodingController {
    @Resource
    RecodingService recodingService;
    @Resource
    UserService userService;
    @Resource
    AccenderService accenderService;

    @RequestMapping(value = "recoding/addRecoding", method = RequestMethod.POST)
    public ModelAndView addRecoding(@RequestParam("id") int id, @RequestParam("username") String username, @RequestParam("contact") String contact,
                                    @RequestParam("address") String address, @RequestParam("question") String question, @RequestParam("date") Date date,
                                    @RequestParam("accender_name") String accender_name, @RequestParam("sender") String sender) throws Exception {
        recodingService.addRecoding(id, username, question, contact, address, date, accender_name, sender);
        userService.deleteUserById(id);
        try {
            if (accenderService.queryEmailByName(accender_name)!=null){
                String e_mail=accenderService.queryEmailByName(accender_name);
                SendMail.sendEMail(e_mail,"出勤任务",sender+"已为你派单"+"请准时完成出勤任务，祝你好运");
            }else {
                SendMail.sendEMail("1426061489@qq.com","错误数据","派出了一条错误数据，请前往数据库查看");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/user/listUser");
        return modelAndView;
    }

    @RequestMapping(value = "recoding/listRecoding",method = RequestMethod.GET)
    public ModelAndView listRecoding(ModelAndView modelAndView) {
        List<Recoding> recodingList = recodingService.recodingList();
        List<User> userList=userService.queryUserList();
        modelAndView.addObject("recodingList", recodingList);
        modelAndView.addObject("userList",userList);
        modelAndView.setViewName("listRecoding");
        return modelAndView;
    }
}
